package com.school.base.action.admin;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;

import com.school.base.model.Subject;
import com.school.base.model.Teacher;
import com.school.base.service.SubjectService;
import com.school.base.service.TeacherService;
import com.school.common.base.BaseAction;
import com.school.common.constant.Constant;
import com.school.common.util.SpringUtils;

public class TeacherAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2131573413338235981L;
	private static Logger logger = Logger.getLogger(TeacherAction.class);
	private static TeacherService service = (TeacherService) SpringUtils.getBean("base_teacherService");
	private Teacher teacher;
	private List<Teacher> teacherList;

	public List<Subject> getSubjectList() {
		SubjectService subjectService = (SubjectService) SpringUtils.getBean("base_subjectService");
		return subjectService.getAll();
	}

	public Teacher getTeacher() {
		return teacher;
	}

	public void setTeacher(Teacher teacher) {
		this.teacher = teacher;
	}

	public List<Teacher> getTeacherList() {
		return teacherList;
	}

	public void setTeacherList(List<Teacher> teacherList) {
		this.teacherList = teacherList;
	}

	public String list() {
		teacherList = service.getTeacherList(teacher, pageBean);
		return SUCCESS;
	}

	public String edit() {
		if (teacher != null && teacher.getId() > 0) {
			teacher = service.getTeacherById(teacher.getId());
		} else {
			teacher = new Teacher();
			teacher.setGender(Constant.GENDER_MAN);
		}
		return SUCCESS;
	}

	public String editDo() {
		if ("POST".equals(this.getHttpServletRequest().getMethod())) {
			if (teacher != null) {
				if (teacher.getId() > 0) {
					try {
						service.updateTeacher(teacher);
						logger.info("Update teacher.");
					} catch (Exception e) {
						this.addActionError(e.getMessage());
						return INPUT;
					}
				} else {
					try {
						service.insertTeacher(teacher);
						logger.info("Insert teacher.");
					} catch (Exception e) {
						this.addActionError(e.getMessage());
						return INPUT;
					}
				}
			} else {
				logger.info("Not save teacher:teacher object is null.");
			}
		} else {
			logger.info("Not save teacher:the request method is not 'post'.");
		}
		return SUCCESS;
	}

	public String delete() {
		if (teacher != null && teacher.getId() > 0) {
			service.deleteTeacher(teacher.getId());
			logger.info("Delete teacher.");
		} else {
			logger.info("Not delete teacher:teacher's id is null.");
		}
		return SUCCESS;
	}

	public void checkLoginName() throws IOException {
		HttpServletResponse response = this.getHttpServletResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		if (teacher != null && teacher.getLoginName() != null && !teacher.getLoginName().isEmpty()) {
			String str = service.checkLoginName(teacher.getLoginName(), teacher.getId()) ? "false" : "true";
			response.getWriter().print(str);
		}
		response.getWriter().print("false");
	}

	public void ajaxGetTeachers() throws IOException {
		HttpServletResponse response = this.getHttpServletResponse();
		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		if (teacher != null && teacher.getRealName() != null && !teacher.getRealName().isEmpty()) {
			List<Teacher> list = service.getTeacherListByName(teacher.getRealName());
			if (list != null && list.size() > 0) {
				JSONArray array = new JSONArray();
				for (Teacher teacher : list) {
					JSONObject obj = new JSONObject();
					obj.put("id", teacher.getId());
					obj.put("realName", teacher.getRealName());
					array.add(obj);
				}
				response.getWriter().print(array);
			}
		}
	}
}
